package com.bw.spsy.pay.controller;

import com.alibaba.fastjson2.JSON;
import com.bw.spsy.pay.domain.PaymentOrder;
import com.bw.spsy.pay.order.WeiXinUtil;
import com.bw.spsy.pay.service.PaymentOrderService;
import com.bw.spsy.pay.vo.CreateOrderVo;
import com.bwie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author: liuzhiping
 * @date: 2025/5/22 16:33
 **/
@RestController
@RequestMapping("/pay")
@Slf4j
public class PayController {

    @Autowired
    private PaymentOrderService paymentOrderService;

    @GetMapping("/createOrder")
    public R createOrder(@RequestBody CreateOrderVo vo) throws Exception{



        log.info("参数 vo:{}", JSON.toJSONString(vo));


        /*PaymentOrder pay = new PaymentOrder();
        pay.setAmount();*/

        PaymentOrder paymentOrder = PaymentOrder.builder()
                .amount(new BigDecimal("0"))
                .createTime(new Date())
                .updateTime(new Date())
                .currency("cny")
                .orderNo(vo.getOrderId())
                .merchantId( (long)100001)
                .expireTime(new Date())
                .subject("测试")
                .userId(Long.valueOf(vo.getUserId()))
                .returnUrl("www.baidu.com")
                .notifyUrl("www.baidu.com")
                .amount(new BigDecimal(vo.getPrice()))
                .status(1)
                .body("测试数据").build();


        log.info("参数 paymentOrder:{}", JSON.toJSONString(paymentOrder));

       boolean boo =  paymentOrderService.save(paymentOrder);

       log.info("boo:{}",boo);

        String url = WeiXinUtil.payOrder(vo.getOrderId());


        log.info("url:{}",url);


        return R.OK(url);
    }
}
